package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.service.ModuleDAO;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class CreateModuleBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private Module module = new Module();
	ModuleDAO moduleDAO = new ModuleDAO();
	
	private boolean FORM_VALID;
    
	public 	CreateModuleBean(){
		System.out.println("INTIALIZING./////////..");
    }
	
	public void saveModule(ActionEvent e){	
		validateForm();
		if(FORM_VALID){
			module.setCreateDate(new Date());
			module.setCreatedBy( "DEVELOPER");
			module.setStatus("ACTIVE");
			moduleDAO.createModule(module);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, module.getModuleName() + " module created successfully.", ""));
			module = new Module();
		}else{
			
		}
		System.out.println("IN SAVE MODULE");
	}
	

	private void validateForm(){
		FORM_VALID = true;
		
		if(ValidationUtil.isEmpty(module.getModuleName(), "Module Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(module.getDisplayOrder(), "Display Order", "moduleForm:displayOrder", FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}
}
